﻿using Microsoft.SharePoint.Client;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace PVReportDeployer
{
    /// <summary>
    /// Interaction logic for ChooseSharePointSource.xaml
    /// </summary>
    public partial class UploadPVWindow : Window
    {
        private ObservableCollection<string> leftCollection;
        private ObservableCollection<string> rightCollection;
        public string deployFolder;
        public UploadPVWindow()
        {
            InitializeComponent();
            this.deployFolder = System.IO.Path.GetTempPath() + "PVReportDeploy" + System.IO.Path.DirectorySeparatorChar;
            this.leftCollection = new ObservableCollection<string>();
            this.rightCollection = new ObservableCollection<string>();
            this.leftList.ItemsSource = leftCollection;
            this.rightList.ItemsSource = rightCollection;
            this.loadPVReport();
        }

        /// <summary>
        /// Upload files to SharePoint.
        /// </summary>
        private void UploadBtn_Click(object sender, RoutedEventArgs e)
        {
            foreach (var item in this.rightCollection)
            {
                uploadPVReport(item);
            }

            // close  windows
            this.DialogResult = true;
            this.Close();
        }


        /// <summary>
        /// Load PowerView reports from deploy folder.
        /// </summary>
        public void loadPVReport()
        {
            string[] filePaths = Directory.GetFiles(this.deployFolder, "*.rdlx");
            foreach (string file in filePaths)
            {
                string fileName = System.IO.Path.GetFileName(file);
                this.leftCollection.Add(fileName);
            }
        }
        /// <summary>
        /// Item to right.
        /// </summary>
        private void toRight_Click(object sender, RoutedEventArgs e)
        {
            var items = this.leftList.SelectedItems;
            var itemList = new List<string>();
            foreach (var item in items)
            {
                itemList.Add(item as string);
            }
            foreach (var item in itemList)
            {
                this.rightCollection.Add(item);
                this.leftCollection.Remove(item);
            }
        }
        /// <summary>
        ///  Item to left.
        /// </summary>
        private void toLeft_Click(object sender, RoutedEventArgs e)
        {
            var items = this.rightList.SelectedItems;
            var itemList = new List<string>();
            foreach (var item in items)
            {
                itemList.Add(item as string);
            }
            foreach (var item in itemList)
            {
                this.rightCollection.Remove(item);
                this.leftCollection.Add(item);
            }
        }

       /// <summary>
       /// Upload PowerView report to SharePoint
       /// </summary>
       /// <param name="fileName">report name</param>
        private void uploadPVReport(string fileName)
        {
            // do upload
            try
            {
                using (ClientContext client = new ClientContext(this.baseURL.Text))
                {
                    SecureString password = new SecureString();
                    client.Credentials = new NetworkCredential(this.userTB.Text, this.psdTB.SecurePassword, this.domainTB.Text);
                    string reportDst = this.baseURL.Text + "/" + this.foldRelativeURL.Text + "/" + fileName;
                    string reportSrc = this.deployFolder + fileName;
                     Uri dstUri = new Uri(this.baseURL.Text + "/" + this.foldRelativeURL.Text + "/" + fileName);
                    using (FileStream fs = new FileStream(reportSrc, FileMode.Open))
                    {
                        Microsoft.SharePoint.Client.File.SaveBinaryDirect(client, dstUri.AbsolutePath, fs, true);
                    }
                }
            }
            catch (Exception err)
            {
                MessageBox.Show(err.Message, "Upload " + fileName + " Failed !");
            }
        }



    }
}
